package com.vm.osm.business.controller;

import com.vm.osm.business.common.ControllerExceptionHandler;
import com.vm.osm.business.dto.app.rsp.RspAppOrderPage;
import com.vm.osm.business.dto.app.rsp.RspProblemPage;
import com.vm.osm.business.dto.req.orderInst.*;
import com.vm.osm.business.dto.req.other.ReqSearchEqu;
import com.vm.osm.business.dto.req.other.ReqSearchEquPart;
import com.vm.osm.business.dto.rsp.RspPage;
import com.vm.osm.business.dto.rsp.failType.RspFailType;
import com.vm.osm.business.dto.rsp.orderInst.AppOrderList;
import com.vm.osm.business.service.*;
import com.vm.osm.business.util.UserDataUtil;
import com.vma.eams.spring.boot.annotation.LogFunc;
import com.vma.eams.spring.boot.annotation.LogModule;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.java.Log;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * Created by chenzui on 2018/4/12.
 */
@RestController
@RequestMapping("/v1.0")
@LogModule(type = "order",name = "工单管理")
@Api(value = "AppOrderController",description = "客户端工单接口",tags = {"AppOrderController"})
public class AppOrderController extends ControllerExceptionHandler {
    private Logger LOG = Logger.getLogger(this.getClass());
    @Autowired
    private IAppOrderService appOrderService;

    @Autowired
    private IMyOrderInfoService myOrderInfoService;

    @Value("${eam_api_url}")
    private String eamApiUrl;
    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private IFailTureService failTureService;

    @Autowired
    private IOrderService orderService;

    @Autowired
    private ICheckPlanInstService checkPlanInstService;

    @ApiOperation(value = "首页获取我的工单列表",notes = "获取我的工单列表--指派给我的。我可以抢的")
    @RequestMapping(value = "/app/order/list",method = RequestMethod.GET)
    public RspAppOrderPage queryMyOrder(@ApiParam(value = "page_num") @RequestParam(value = "page_num") Integer pageNum
            , @ApiParam(value = "page_size") @RequestParam(value = "page_size") Integer pageSize, HttpServletRequest request){
        LOG.info("获取我的工单列表");

        return appOrderService.findPage(UserDataUtil.getStaffId(request),pageNum,pageSize,UserDataUtil.getUserSelectedEnterpriseId(request));
    }

    //================================================故障工单操作=======================================================
    //==============接单，拒绝接单，抢单，误报，上报，指派，受理，拒绝受理，转派给外部服务商，处理完成，确认，退回，撤销，评价
    @LogFunc(type = "accept",name = "接单")
    @ApiOperation(value = "故障工单接单",notes = "故障工单接单")
    @RequestMapping(value = "/app/fail/accept",method = RequestMethod.PUT)
    public void agree(HttpServletRequest request,
                      @ApiParam(value = "order_id",name="order_id",required = true) @RequestParam("order_id") String orderId){
        LOG.info("故障单接单");
        myOrderInfoService.agree(orderId,UserDataUtil.getStaffId(request),UserDataUtil.getStaffName(request),UserDataUtil.getStaffHeadImg(request));
    }
    @LogFunc(type = "refuse",name = "拒绝工单")
    @ApiOperation(value = "故障工单拒绝接单",notes = "故障工单拒绝接单，拒绝接受指派给自己的")
    @RequestMapping(value = "/app/fail/refuse",method = RequestMethod.PUT)
    public void disAgree(@ApiParam(value = "order_id",name="order_id",required = true) @RequestParam("order_id") String orderId,
                         @ApiParam(value = "type",name="type",required = true) @RequestParam("type") String type,
                         @ApiParam(value = "reason",name="reason",required = true) @RequestParam("reason") String reason,
                         HttpServletRequest request){
        LOG.info("故障单拒绝接单");
        myOrderInfoService.disagree(orderId,type,reason,UserDataUtil.getStaffId(request),UserDataUtil.getStaffName(request),UserDataUtil.getStaffHeadImg(request));
    }
    @ApiOperation(value = "故障工单抢单",notes = "故障工单抢单")
    @RequestMapping(value = "/app/fail/catch/{id}",method = RequestMethod.PUT)
    public void catchOrder(
            HttpServletRequest request,
            @ApiParam(value = "id",name = "id") @PathVariable(value = "id") String id){
        LOG.info("抢单");

        myOrderInfoService.catchOrder(id,UserDataUtil.getStaffId(request),UserDataUtil.getStaffName(request),
                UserDataUtil.getStaffHeadImg(request));
    }
    @LogFunc(type = "mistake",name = "误报")
    @ApiOperation(value = "故障误报",notes = "故障误报")
    @ApiImplicitParam(value = "reqBackOrder",name = "reqBackOrder",dataType = "ReqBackOrder",required = true)
    @RequestMapping(value = "/app/fail/mistake",method = RequestMethod.PUT)
    public void back(@RequestBody ReqBackOrder reqBackOrder,HttpServletRequest request){
        LOG.info("故障误报");
        reqBackOrder.setHead_icon(UserDataUtil.getStaffHeadImg(request));
        reqBackOrder.setStaff_id(UserDataUtil.getStaffId(request));
        reqBackOrder.setStaff_name(UserDataUtil.getStaffName(request));
        myOrderInfoService.back(reqBackOrder);
    }

    @LogFunc(type = "up",name = "上报")
    @ApiOperation(value = "故障上报",notes = "故障上报")
    @ApiImplicitParam(value = "reqUpOrder",name = "reqUpOrder",dataType = "ReqUpOrder",required = true)
    @RequestMapping(value = "/app/fail/up",method = RequestMethod.POST)
    public void upOrder(@RequestBody ReqUpOrder reqUpOrder,HttpServletRequest request){
        LOG.info("故障上报");
        reqUpOrder.setHead_icon(UserDataUtil.getStaffHeadImg(request));
        reqUpOrder.setStaff_id(UserDataUtil.getStaffId(request));
        reqUpOrder.setStaff_name(UserDataUtil.getStaffName(request));
        myOrderInfoService.up(reqUpOrder);
    }

    @LogFunc(type = "dispatch",name = "转派工单")
    @ApiOperation(value = "上报完，内外部分配公司或者员工",notes = "上报中的订单进行再次派单")
    @ApiImplicitParam(value = "reqAssignOrder",name = "reqAssignOrder",dataType = "ReqAssignOrder",required = true)
    @RequestMapping(value = "/app/fail/dispatch",method = RequestMethod.PUT)
    public void dealUpOrder(@RequestBody ReqAssignOrder reqAssignOrder,HttpServletRequest request){
        LOG.info("上报中的订单进行再次派单");
        reqAssignOrder.setOperate_staff_icon(UserDataUtil.getStaffHeadImg(request));
        reqAssignOrder.setOperate_staff_id(UserDataUtil.getStaffId(request));
        reqAssignOrder.setOperate_staff_name(UserDataUtil.getStaffName(request));
        myOrderInfoService.dealUpOrder(reqAssignOrder,request);
    }
    @LogFunc(type = "accept",name = "受理工单")
    @ApiOperation(value = "受理外部单据",notes = "受理外部单据")
    @ApiImplicitParam(name = "reqAcceptByDept",value = "reqAcceptByDept",dataType = "ReqAcceptByDept")
    @RequestMapping(value = "/app/out/accept",method = RequestMethod.PUT)
    public void acceptByDept(@RequestBody ReqAcceptByDept reqAcceptByDept,HttpServletRequest request){
        LOG.info("受理外部单据");
//        reqAcceptByDept.setStaff_icon(UserDataUtil.getStaffHeadImg(request));
//        reqAcceptByDept.setStaff_id(UserDataUtil.getStaffId(request));
//        reqAcceptByDept.setStaff_name(UserDataUtil.getStaffName(request));
        orderService.acceptByDept(reqAcceptByDept,request);
    }

    @LogFunc(type = "accept",name = "受理工单")
    @ApiOperation(value = "批量受理受理外部单据",notes = "批量受理受理外部单据")
    @ApiImplicitParam(name = "reqAcceptByDeptList",value = "reqAcceptByDeptList",dataType = "List")
    @RequestMapping(value = "/app/out/accept/list",method = RequestMethod.PUT)
    public void acceptByDeptBatch(@RequestBody List<ReqAcceptByDept> reqAcceptByDeptList,HttpServletRequest request){
        LOG.info("受理外部单据");
        for(ReqAcceptByDept reqAcceptByDept:reqAcceptByDeptList){
            reqAcceptByDept.setStaff_icon(UserDataUtil.getStaffHeadImg(request));
            reqAcceptByDept.setStaff_id(UserDataUtil.getStaffId(request));
            reqAcceptByDept.setStaff_name(UserDataUtil.getStaffName(request));
            if(reqAcceptByDept.getOrder_type().equals("1")){
                orderService.acceptByDept(reqAcceptByDept,request);
            }else {
                checkPlanInstService.outAccept(reqAcceptByDept,request);
            }
        }

    }

    @LogFunc(type = "refuse",name = "拒绝工单")
    @ApiOperation(value = "拒绝受理外部单据",notes = "拒绝受理外部单据")
    @ApiImplicitParam(name = "reqRefuseByDept",value = "reqRefuseByDept",dataType = "ReqRefuseByDept")
    @RequestMapping(value = "/app/out/refuse",method = RequestMethod.PUT)
    public void refuseByDept(@ApiParam(value = "order_id",name="order_id",required = true) @RequestParam("order_id") String orderId,
                             @ApiParam(value = "type",name="type",required = true) @RequestParam("type") String type,
                             @ApiParam(value = "reason",name="reason",required = true) @RequestParam("reason") String reason
            ,HttpServletRequest request){
        LOG.info("拒绝受理外部单据");
        orderService.refuseByDept(orderId,type,reason,request);
    }

    @LogFunc(type = "deal",name = "处理完成工单")
    @ApiOperation(value = "接单后填写工单信息",notes = "接单后填写工单信息，直接处理完成或者保存信息")
    @ApiImplicitParam(name = "reqDealOrderFailure",value = "请求参数",required = true,dataType = "ReqDealOrderFailure" )
    @RequestMapping(value = "/app/fail/deal",method = RequestMethod.POST)
    public void updateOrderFailure(@RequestBody ReqDealOrderFailure reqDealOrderFailure, HttpServletRequest request){
        String staffId = UserDataUtil.getStaffId(request);
        String staffName = UserDataUtil.getStaffName(request);
        String headIcon = UserDataUtil.getStaffHeadImg(request);
        reqDealOrderFailure.setStaff_id(staffId);
        reqDealOrderFailure.setStaff_name(staffName);
        reqDealOrderFailure.setHead_icon(headIcon);
        myOrderInfoService.updateOrderFailure(reqDealOrderFailure);
    }
    @LogFunc(type = "back",name = "退回故障处理")
    @ApiOperation(value = "退回",notes = "处理的不行，退回重新处理")
    @RequestMapping(value = "/app/fail/back/{id}",method = RequestMethod.PUT)
    public void again(HttpServletRequest request,
                          @ApiParam(value = "remark",name = "remark",required = false) @RequestParam(value = "remark",name = "remark",required = false)  String remark,
                          @ApiParam(value = "id",name = "id") @PathVariable(value = "id") String id){
        LOG.info("退回");
        appOrderService.again(request,id,remark);
    }
    @LogFunc(type = "confirm",name = "确认故障处理")
    @ApiOperation(value = "确认",notes = "确认,订单完成，进入评价环节")
    @RequestMapping(value = "/app/fail/confirm/{id}",method = RequestMethod.PUT)
    public void confirm(HttpServletRequest request,
                          @ApiParam(value = "id",name = "id") @PathVariable(value = "id") String id){
        LOG.info("确认");
        appOrderService.confirm(request,id);
    }

    @ApiOperation(value = "撤销",notes = "故障单申请人撤销")
    @RequestMapping(value = "/app/fail/cancel/{id}",method = RequestMethod.PUT)
    public void cancel(HttpServletRequest request,
                        @ApiParam(value = "id",name = "id") @PathVariable(value = "id") String id){
        LOG.info("撤销");
        appOrderService.cancel(request,id);
    }

    @ApiOperation(value = "撤回",notes = "撤销上报，继续自己处理")
    @LogFunc(type = "turn_back",name = "撤回故障申报")
    @RequestMapping(value = "/app/fail/turn_back/{id}",method = RequestMethod.PUT)
    public void turnBack(HttpServletRequest request,
                         @ApiParam(value = "remark",name = "remark",required = false) @RequestParam(value = "remark",name = "remark",required = false)  String remark,
                         @ApiParam(value = "id",name = "id") @PathVariable(value = "id") String id){
        LOG.info("撤回");
        appOrderService.turnBack(request,id,remark);
    }
    //================================================故障工单操作======================================================




    @ApiOperation(value = "查询设备接口",notes = "查询设备接口")
    @ApiImplicitParam(name = "reqSearchEqu",value = "查询设备接口参数",required = true,dataType = "ReqSearchEqu")
    @RequestMapping(value = "/app/equ_list",method = RequestMethod.POST)
    public Object queryEquInfo(HttpServletRequest request, @RequestBody ReqSearchEqu reqSearchEqu){
        String enterpriseId = UserDataUtil.getUserSelectedEnterpriseId(request);
        LOG.info("enterpriseId"+enterpriseId);
        // TODO: 2018/2/22  根据企业编号调用设备类型接口
        String postUrl = eamApiUrl+"/eam/v1.0/"+enterpriseId+"/equipments/search";

        return restTemplate.postForObject(postUrl,reqSearchEqu,Object.class);
    }

    @ApiOperation(value = "获取企业员工配件列表",notes = "http://120.55.12.229:9001/eam/swagger-ui.html#!/EquPartInfoController/getEnterpriseStaffEquPartPageUsingPOST")
    @ApiImplicitParam(name = "reqSearchEquPart",value = "reqSearchEquPart参数",dataType = "ReqSearchEquPart")
    @RequestMapping(value = "/app/equ/accessory",method = RequestMethod.POST)
    public Object getEqu(HttpServletRequest request, @RequestBody ReqSearchEquPart reqSearchEquPart){
        String enterpriseId = UserDataUtil.getUserSelectedEnterpriseId(request);
        String staffId = UserDataUtil.getStaffId(request);
        String postUrl = eamApiUrl+"/eam/v1.0/"+enterpriseId+"/"+staffId+"/equ_parts/search";
        return restTemplate.postForObject(postUrl,reqSearchEquPart,Object.class);
    }


    @ApiOperation(value = "获取故障类型树",notes = "获取故障类型树")
    @RequestMapping(value = "/app/fail_type/list",method = RequestMethod.GET)
    public List<RspFailType> getFailTypes(HttpServletRequest request){
        return failTureService.findList("0",UserDataUtil.getUserSelectedEnterpriseId(request));
    }

    @ApiOperation(value = "获取问题库列表",notes = "获取问题库列表")
    @RequestMapping(value = "/app/problem/list",method = RequestMethod.GET)
    public RspProblemPage getList(
            @ApiParam(value = "title",name="title",required = false) @RequestParam(value = "title",required = false) String title,
            @ApiParam(value = "failure_type_id",name="failure_type_id",required = false) @RequestParam(value = "failure_type_id",required = false) String failureTypeId,
            @ApiParam(value = "page_num",name="page_num",required = true) @RequestParam("page_num") Integer pageNum,
            @ApiParam(value = "page_size",name="page_size",required = true) @RequestParam("page_size") Integer pageSize,
            HttpServletRequest request){
        return appOrderService.findPage(UserDataUtil.getStaffId(request),title,failureTypeId,pageNum,pageSize);
    }

    @ApiOperation(value = "首页数据统计",notes = "1今日完成2今日接单3本月完成")
    @RequestMapping(value = "/app/index/data",method = RequestMethod.GET)
    public Map indexData(HttpServletRequest request){
        return appOrderService.data(UserDataUtil.getStaffId(request));
    }

    @ApiOperation(value = "获取我的工单统计数据",notes = "获取我的工单统计数据")
    @RequestMapping(value = "/app/order/status_data",method = RequestMethod.GET)
    public Map queryStatusData(HttpServletRequest request){
        return appOrderService.queryStatus(UserDataUtil.getStaffId(request));
    }

    @ApiOperation(value = "获取工单管理模块，工单统计数据",notes = "获取工单管理模块，工单统计数据")
    @RequestMapping(value = "/app/order/status_dept_data",method = RequestMethod.GET)
    public Map queryStatusDataByDept(HttpServletRequest request){
        return appOrderService.queryStatusByDept(UserDataUtil.getDepartmentId(request),UserDataUtil.getUserSelectedEnterpriseId(request));
    }

    @ApiOperation(value = "我的工单列表",notes = "我的工单列表")
    @RequestMapping(value = "/app/my/order",method = RequestMethod.GET)
    public RspPage<AppOrderList> queryAppMyOrder(
            @ApiParam(value = "1后期2遗留3今日",name="type",required = false) @RequestParam(value = "type",required = false) String type,
            @ApiParam(value = "page_num",name="page_num",required = true) @RequestParam("page_num") Integer pageNum,
            @ApiParam(value = "page_size",name="page_size",required = true) @RequestParam("page_size") Integer pageSize,
            HttpServletRequest request){
        return appOrderService.queryAppMyOrder(UserDataUtil.getStaffId(request),UserDataUtil.getUserSelectedEnterpriseId(request),
               type,pageNum,pageSize );
    }

    @ApiOperation(value = "工单管理列表",notes = "工单管理列表")
    @RequestMapping(value = "/app/department/order",method = RequestMethod.GET)
    public RspPage<AppOrderList> queryAppDepartmentOrder(
            @ApiParam(value = "全部传，待受理分配传3,12  待接单待抢单传4,5  处理中传6  已完成传8",name="type",required = false) @RequestParam(value = "type",required = false) String type,
            @ApiParam(value = "page_num",name="page_num",required = true) @RequestParam("page_num") Integer pageNum,
            @ApiParam(value = "page_size",name="page_size",required = true) @RequestParam("page_size") Integer pageSize,
            HttpServletRequest request){
        return appOrderService.queryAppDepartmentOrder(UserDataUtil.getUserSelectedEnterpriseId(request),
                type,pageNum,pageSize );
    }

    @ApiOperation(value = "修改预约时间",notes = "修改预约时间")
    @RequestMapping(value = "/app/order/modify_time",method = RequestMethod.POST)
    public void modifyTime(@RequestBody ReqModifyTime reqModifyTime,HttpServletRequest request){
        appOrderService.modifyTime(reqModifyTime,request);
    }
}
